﻿using Models.Mailbox.Longin;
using Newtonsoft.Json;
using SendMessagePage.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SendMessagePage
{
    /// <summary>
    /// PackingUserLogin.xaml 的交互逻辑
    /// </summary>
    public partial class PackingUserLogin : Window
    {

        //查询任务是否存在
        private string AdminLoginUrl = ConfigurationManager.AppSettings["AdminLogin"];

        public static string token = string.Empty;

        //全局登录对象
        public static   LoginResult loginResult = null;

        /// <summary>
        /// 用户
        /// </summary>
        public static UserLogin User { get; set; }

        public PackingUserLogin()
        {
            InitializeComponent();
            this.txtUid.Focus();
        }

        private async void login_Click(object sender, RoutedEventArgs e)
        {
            string loginUid = this.txtUid.Text.Trim();
            string loginPwd = this.txtPwd.Password.ToString().Trim();
            //ckChecked = ckPwd.IsChecked.Value;

            if (loginUid.Length == 0)
            {
                MessageBox.Show("账号不能为空", "登录提示");
                this.txtUid.Focus();
                return;
            }

            if (loginPwd.Length == 0)
            {
                MessageBox.Show("密码不能为空", "登录提示");
                this.txtPwd.Focus();
                return;
            }
            User = new UserLogin()
            { 
                UserName = loginUid,
                PassWord = loginPwd
             };


  

            // GetLoginResultAsync(user);

            string json = JsonConvert.SerializeObject(User);


            string result = await AccessWebApiUtil.PostAsyncJson(AdminLoginUrl, json);


            loginResult = JsonConvert.DeserializeObject<LoginResult>(result);

         

            if (loginResult.Code == 200)
            {

                token = loginResult.Result.Token;
                MainWindow window = new MainWindow();
                window.Show();
                this.Close();
            }
            else {

                MessageBox.Show("用户名或密码错误！", "提示信息");
                this.txtPwd.Password = string.Empty;

            }




        }


        public async void  GetLoginResultAsync(UserLogin user ) {

            string json = JsonConvert.SerializeObject(user);


            string result = await AccessWebApiUtil.PostAsyncJson(AdminLoginUrl, json);


            loginResult = JsonConvert.DeserializeObject<LoginResult>(result);


        }

        protected override void OnKeyDown(KeyEventArgs e)
        {

            if (e.Key == Key.Enter)
            {
                // MoveFocus takes a TraveralReqest as its argument.
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

                // Gets the element with keyboard focus.
                UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;

                // Change keyboard focus.
                if (elementWithFocus != null)
                {
                    elementWithFocus.MoveFocus(request);
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }



    }
}
